{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82a178b6",
   "metadata": {},
   "source": [
    "### 输入框架"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fd18721",
   "metadata": {},
   "source": [
    "### 输出框架"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cad9ede",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(*arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4baee12",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(*objects, sep=' ', end='\\n')\n",
    "# sep是分隔符，默认是空格. end是结尾，默认是换行符."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "239389df",
   "metadata": {},
   "outputs": [],
   "source": [
    "%s 字符串   \n",
    "%x 将18保存为16进制的格式\n",
    "%d 整数    \n",
    "%.2f 浮点数字(用小数点符号，4舍5入)   \n",
    "%.2e 浮点数字(科学计数法)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1953016",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('%x'%18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a970877",
   "metadata": {},
   "outputs": [],
   "source": [
    "pi = 3.1415926\n",
    "print('%.3f' % pi)  # 3.142"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa2b709",
   "metadata": {},
   "source": [
    "### IDLE的用法"
   ]
  },
  {
   "cell_type": "raw",
   "id": "41c7ac54",
   "metadata": {},
   "source": [
    "ctrl + n 新建 python 文件， F5 运行。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9982d70",
   "metadata": {},
   "source": [
    "### 常用标准库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd18e908",
   "metadata": {},
   "outputs": [],
   "source": [
    "math\n",
    "数论\n",
    "math.ceil(3.5)  # 4 向上取整\n",
    "math.floor(3.5)  # 3 向下取整\n",
    "math.fabs(-3)  # 3.0 绝对值\n",
    "math.factorial(4)  # 24 阶乘\n",
    "math.comb(4, 2)  # 6(组合数)\n",
    "math.perm(4, 2)  # 12（排列数）\n",
    "math.isqrt(5)  # 2(根号5向下取整)\n",
    "math.gcd(4, 6)  # 2(最大公约数)\n",
    "math.lcm(4, 6)  # 12(最小公倍数)\n",
    "math.prod([2, 3, 5])  # 30 乘积\n",
    "math.isfinite(x)  # x既不是无穷大也不是NaN返回True\n",
    "math.isinf(x)  # x是正或负无穷大，返回True\n",
    "math.isnan(x)  # x是NaN，返回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a25f72a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "数学函数(结果均为浮点数)\n",
    "math.exp(x)          # 返回 e 次 x 幂\n",
    "math.expm1(x)        # 返回 e 的 x 次幂，减1\n",
    "math.log(x[, base])  \n",
    "# 使用一个参数，返回 x 的自然对数（底为 e ）。\n",
    "# 使用两个参数，返回给定的 base 的对数 x ，计算为 log(x)/log(base)\n",
    "math.pow(x, y)  # x的y次方\n",
    "math.sqrt(x)    # x 的平方根\n",
    "math.sin(x)\n",
    "math.cos(x)\n",
    "math.tan(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea0c9c02",
   "metadata": {},
   "source": [
    "### 常数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b7b925b",
   "metadata": {},
   "outputs": [],
   "source": [
    "math.pi\n",
    "math.e\n",
    "math.inf  # 浮点正无穷大，对于负无穷大，使用-math.inf，相当于float('inf')\n",
    "math.nan  # 浮点非数字(NaN)值，相当于float('nan')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4f5a856",
   "metadata": {},
   "source": [
    "###  random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23172715",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.random()  # 返回[0.0, 1.0)范围内的一个随机浮点数\n",
    "random.randint(a, b)  # 返回随机整数N满足a<=N<=b\n",
    "random.choice(seq)  # 从非空序列seq中随机选取一个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "422b6bfe",
   "metadata": {},
   "source": [
    "### collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8949ed7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "collections.Counter('aabbccc')  # Counter({'c': 3, 'a': 2, 'b': 2})\n",
    "collections.Counter(['a', 'a', 'b'])  # Counter({'a': 2, 'b': 1})\n",
    "collections.Counter(cats=4, dogs=8)  # Counter({'dogs': 8, 'cats': 4})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab72bf30",
   "metadata": {},
   "outputs": [],
   "source": [
    "import collections\n",
    "data = collections.Counter('aabbccc')\n",
    "print(data['a'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a0e3969",
   "metadata": {},
   "source": [
    "### elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7589607",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "data = collections.Counter(a=4, b=2, c=0, d=-2)\n",
    "\n",
    "data = list(data.elements())\n",
    "print(data)  # ['a', 'a', 'a', 'a', 'b', 'b']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fd50418",
   "metadata": {},
   "source": [
    "### most_common"
   ]
  },
  {
   "cell_type": "raw",
   "id": "42538b75",
   "metadata": {},
   "source": [
    "返回一个列表，其中包含 n 个最常见的元素及出现次数，按常见程度由高到低排序。 \n",
    "如果 n 被省略或为 None，most_common() 将返回计数器中的 所有 元素。 \n",
    "计数值相等的元素按首次出现的顺序排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6580a486",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "data = collections.Counter('abracadabra')\n",
    "print(data.most_common(3))  # [('a', 5), ('b', 2), ('r', 2)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7bf7b7d",
   "metadata": {},
   "source": [
    "### subtract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09ae2e44",
   "metadata": {},
   "outputs": [],
   "source": [
    "从 迭代对象 或 映射对象 减去元素。输入和输出都可以是0或者负数。\n",
    "c = Counter(a=4, b=2, c=0, d=-2)\n",
    "d = Counter(a=1, b=2, c=3, d=4)\n",
    "print(c.subtract(d))  # Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61892a73",
   "metadata": {},
   "source": [
    "### 补充前导0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fe8e608",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "string = '33'\n",
    "print(string.zfill(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4640e32b",
   "metadata": {},
   "source": [
    "### 列表转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e899495b",
   "metadata": {},
   "outputs": [],
   "source": [
    "列表的\"*\"操作\n",
    "\n",
    "在迭代对象前使用‘*’号，将变量中可迭代对象的元素拆解出来，变成若干独立的参数。\n",
    "\n",
    "_A = list(map(list, zip(*A))) # list转置\n",
    "\n",
    "zip() 函数用于将可迭代的对象作为参数，将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表。\n",
    "\n",
    "zip 方法在 Python 2 和 Python 3 中的不同：在 Python 3.x 中为了减少内存，zip() 返回的是一个对象。如需展示列表，需手动 list() 转换。\n",
    "\n",
    "map() 会根据提供的函数对指定序列做映射.\n",
    "第一个参数 function 以参数序列中的每一个元素调用 function 函数，返回包含每次 function 函数返回值的新列表。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3c4e74e",
   "metadata": {},
   "source": [
    "### 排列组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "001fe25e",
   "metadata": {},
   "outputs": [],
   "source": [
    " import itertools\n",
    " data = [3, 4, 6]\n",
    "\n",
    " com_res = itertools.combinations(data, 2)\n",
    " print(list(com_res))\n",
    " # [(3, 4), (3, 6), (4, 6)]\n",
    "\n",
    " per_res = itertools.permutations(data, 2)\n",
    " print(list(per_res))\n",
    " # [(3, 4), (3, 6), (4, 3), (4, 6), (6, 3), (6, 4)]\n",
    "\n",
    "\n",
    " com_without_replacement_res = itertools.combinations_with_replacement(data, 2)\n",
    " print(list(com_without_replacement_res))\n",
    " # [(3, 3), (3, 4), (3, 6), (4, 4), (4, 6), (6, 6)]\n",
    "\n",
    " product_res = itertools.product(data, data)\n",
    " print(list(product_res))\n",
    " # [(3, 3), (3, 4), (3, 6), (4, 3), (4, 4), (4, 6), (6, 3), (6, 4), (6, 6)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f3ade2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "combinations 组合\n",
    "permutations 排列\n",
    "\n",
    "combinations_with_replacement 组合可重复\n",
    "product 排列可重复"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3329a5c",
   "metadata": {},
   "source": [
    "### 计算组合\\排列数学问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "400d8b3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import comb\n",
    "print((comb(8, 3)))\n",
    "\n",
    "from math import perm\n",
    "print(perm(8, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "303468c9",
   "metadata": {},
   "source": [
    "### 字符串判定"
   ]
  },
  {
   "cell_type": "raw",
   "id": "50a98b77",
   "metadata": {},
   "source": [
    "字母数字字符串 isalnum\n",
    "字母字符串 isalpha\n",
    "字符都是ASCII isascii\n",
    "十进制字符串 isdecimal\n",
    "数字字符串 isdigit\n",
    "小写字符串 islower\n",
    "大写字符串 isupper\n",
    "数字字符串 isnumeric\n",
    "空白字符串 isspace\n",
    "以标题为大小写的字符串 istitle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9922572d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print([[0] * 10 for i in range(10)])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c29a5fe2",
   "metadata": {},
   "source": [
    "有前缀：\n",
    "\n",
    "x = 123\n",
    "print(bin(x))  # 0b1111011(二进制)\n",
    "print(oct(x))  # 0o173(八进制)\n",
    "print(hex(x))  # 0x7b(十六进制)\n",
    "\n",
    "没有前缀：\n",
    "\n",
    "x = 123\n",
    "print(format(x, 'b'))  # 1111011\n",
    "print(format(x, 'o'))  # 173\n",
    "print(format(x, 'x'))  # 7b\n",
    "\n",
    "任意进制转换为10进制：\n",
    "\n",
    "int(x, a)\n",
    "\n",
    "x 为字符串， 且是 a 进制数。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff633a4",
   "metadata": {},
   "source": [
    "### sorted"
   ]
  },
  {
   "cell_type": "raw",
   "id": "74bdcf4d",
   "metadata": {},
   "source": [
    "sorted(iterable, key=None, reverse=False)  \n",
    "\n",
    "    iterable – 可迭代对象。\n",
    "    key – 主要是用来进行比较的元素，只有一个参数，具体的函数的参数就是取自于可迭代对象中，指定可迭代对象中的一个元素来进行排序。\n",
    "    reverse – 排序规则，reverse = True 降序 ， reverse = False 升序（默认）。\n",
    "\n",
    "dic = [('a', 3), ('c', 1), ('b', 2)]\n",
    "print(sorted(dic, key=lambda x: x[0])) \n",
    "print(sorted(dic, key=lambda x: x[1]))\n",
    "# [('a', 3), ('b', 2), ('c', 1)]\n",
    "# [('c', 1), ('b', 2), ('a', 3)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c397cf5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "exit()常用于多层循环的退出，它可直接将python程序终止，之后的所有代码都不会继续执行。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lanqiaobei",
   "language": "python",
   "name": "lanqiaobei"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
